/*    */ package org.appfuse.util;
/*    */ 
/*    */ import java.text.DecimalFormat;
/*    */ import java.text.ParseException;
/*    */ import org.apache.commons.beanutils.ConversionException;
/*    */ import org.apache.commons.beanutils.Converter;
/*    */ import org.apache.commons.lang.StringUtils;
/*    */ import org.apache.commons.logging.Log;
/*    */ import org.apache.commons.logging.LogFactory;
/*    */ 
/*    */ public class CurrencyConverter
/*    */   implements Converter
/*    */ {
/* 19 */   private final Log log = LogFactory.getLog(CurrencyConverter.class);
/* 20 */   private DecimalFormat formatter = new DecimalFormat("###,###.00");
/*    */ 
/*    */   public void setDecimalFormatter(DecimalFormat df) {
/* 23 */     this.formatter = df;
/*    */   }
/*    */ 
/*    */   public final Object convert(Class type, Object value)
/*    */   {
/* 35 */     if (value == null) {
/* 36 */       return null;
/*    */     }
/* 38 */     if ((value instanceof String)) {
/* 39 */       if (this.log.isDebugEnabled()) {
/* 40 */         this.log.debug("value (" + value + ") instance of String");
/*    */       }
/*    */       try
/*    */       {
/* 44 */         if (StringUtils.isBlank(String.valueOf(value))) {
/* 45 */           return null;
/*    */         }
/*    */ 
/* 48 */         if (this.log.isDebugEnabled()) {
/* 49 */           this.log.debug("converting '" + value + "' to a decimal");
/*    */         }
/*    */ 
/* 53 */         Number num = this.formatter.parse(String.valueOf(value));
/*    */ 
/* 55 */         return Double.valueOf(num.doubleValue());
/*    */       } catch (ParseException pe) {
/* 57 */         pe.printStackTrace();
/*    */       }
/* 59 */     } else if ((value instanceof Double)) {
/* 60 */       if (this.log.isDebugEnabled()) {
/* 61 */         this.log.debug("value (" + value + ") instance of Double");
/* 62 */         this.log.debug("returning double: " + this.formatter.format(value));
/*    */       }
/*    */ 
/* 65 */       return this.formatter.format(value);
/*    */     }
/*    */ 
/* 69 */     throw new ConversionException("Could not convert " + value + " to " + type.getName() + "!");
/*    */   }
/*    */ }

